package com.ekalenchuk.module;

import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ekalenchuk.module.constants.PageConstants;
import com.ekalenchuk.module.domain.Article;
import com.ekalenchuk.module.service.ArticleService;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {

	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

	@Autowired
	private ArticleService articleService;

	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		return PageConstants.REDIRECT_HOME_PAGE;
	}

	@RequestMapping(value = "/start")
	public String startPage(Map<String, Object> model) {
		Article article = new Article();
		article.setCreated(new Date());
		article.setTitle("title" + Math.random());
		article.setText("message" + Math.random());
		articleService.save(article);
		List<Article> articles = articleService.getArticles();
		model.put("articles", articles);
		return PageConstants.HOME_PAGE;
	}

}
